home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / basic / strcmp.lha / strcmp.bb2 next >
Encoding:
Text File  |  1999-07-03  |  2.3 KB  |  98 lines

  1. ; Description:      2 C style string compare routines in ASM
  2. ;                   one is case sensitive the other is insensitive
  3. ;
  4. ; Type:             System
  5. ;
  6. ; Last updated:     3rd July 1999
  7. ;
  8. ; Author:           David McMinn (dmcminn@house-of-mojo.freeserve.co.uk)
  9. ;
  10. ; In both functions, you pass the address of two strings you want
  11. ; to compare. The returned value will be:
  12. ;
  13. ; <0 if s1 < s2
  14. ; =0 if s1 = s2
  15. ; >0 if s1 > s2
  16.  
  17.  
  18. ; Case sensitive compare
  19. Function.w ASM_strcmp{*s1,*s2}
  20. .ASM_strcmp
  21.         MOVEA.l d0,a0
  22.         MOVEA.l d1,a1
  23.  
  24. 'fetch: MOVE.b  (a1)+,d1
  25.         MOVE.b  (a0)+,d0
  26.  
  27.         BEQ     'exit       ; check if letter is null terminator
  28.  
  29.         CMP.b   d0,d1       ; check if letters are same
  30.         BEQ     'fetch      ; if they are, move onto next kletter
  31.  
  32. 'exit:  ANDI.w  #$00FF,d0   ; make sure that top byte of word is clear
  33.         ANDI.w  #$00FF,d1   ; make sure that top byte of word is clear
  34.         SUB.w   d1,d0       ; get return result
  35.         AsmExit
  36. End Function
  37.  
  38.  
  39. ; Case insensitive compare
  40. Function.w ASM_strcmpi{*s1,*s2}
  41. .ASM_strcmpi
  42.         MOVEA.l d0,a0
  43.         MOVEA.l d1,a1
  44.  
  45.         MOVE.b  #96,d2
  46.         MOVE.b  #123,d3
  47.         MOVE.b  #$DF,d4
  48.  
  49. 'fetch: MOVE.b  (a1)+,d1
  50.         MOVE.b  (a0)+,d0
  51.  
  52.         BEQ     'exit       ; check if letter is null terminator
  53.  
  54.         CMP.b   d2,d0
  55.         BLE     'noup1
  56.         CMP.b   d3,d0
  57.         BGE     'noup1
  58.         AND.b   d4,d0
  59.  
  60. 'noup1: CMP.b   d2,d1
  61.         BLE     'noup2
  62.         CMP.b   d3,d1
  63.         BGE     'noup2
  64.         AND.b   d4,d1
  65.  
  66. 'noup2: CMP.b   d0,d1       ; check if letters are same
  67.         BEQ     'fetch      ; if they are, move onto next kletter
  68.  
  69. 'exit:  ANDI.w  #$00FF,d0   ; make sure that top byte of word is clear
  70.         ANDI.w  #$00FF,d1   ; make sure that top byte of word is clear
  71.         SUB.w   d1,d0       ; get return result
  72.         AsmExit
  73. End Function
  74.  
  75.  
  76. ; Small demo
  77.  
  78. a$ = "Hello world"
  79. b$ = "hello world"
  80.  
  81.  
  82. NPrint "Comparing with case sensitivity"
  83. result.w = ASM_strcmp{&a$,&b$}
  84. If result<0 Then NPrint "a$ < b$"
  85. If result=0 Then NPrint "a$ = b$"
  86. If result>0 Then NPrint "a$ > b$"
  87.  
  88. NPrint "Comparing with case insensitivity"
  89. result.w = ASM_strcmpi{&a$,&b$}
  90. If result<0 Then NPrint "a$ < b$"
  91. If result=0 Then NPrint "a$ = b$"
  92. If result>0 Then NPrint "a$ > b$"
  93.  
  94. NPrint "Press mouse button to quit"
  95. ClickMouse
  96. End
  97.  
  98.